package com.qdlc.p2p.biz.task.impl;

import com.qdlc.p2p.biz.service.EnjoyUserService;
import com.qdlc.p2p.biz.task.AbstractLoanTask;
import com.qdlc.p2p.biz.task.JobQueue;
import com.qdlc.p2p.biz.task.ValueEvent;
import com.qdlc.p2p.common.util.BeanUtil;
import org.apache.log4j.Logger;

/**
 * 趣享赠送奖品队列
 * Created by D on 2016/1/21.
 */
public class EnjoyGoodsTask extends AbstractLoanTask {

    private Logger logger = Logger.getLogger(EnjoyGoodsTask.class);

    public EnjoyGoodsTask() {
        super();
        task.setName("EnjoyGoods.Task");
    }

    @SuppressWarnings("unchecked")
    @Override
    public void doLoan() {
        EnjoyUserService enjoyUserService = (EnjoyUserService) BeanUtil.getBean("enjoyUserService");
        JobQueue<ValueEvent> queue = JobQueue.getEnjoyGoodsInstance();
        while (!queue.isEmpty()) {
            ValueEvent event = queue.peek();
            if (event != null) {
                logger.info("给tender为["+event.getEnjoyUser().getTenderId()+"]的投标记录发放商品");
                try {
                    enjoyUserService.giveGoods(event.getEnjoyUser());
                } catch (Exception e) {
                    logger.info("给tenderId为["+event.getEnjoyUser().getTenderId()+"]的投标记录进行发放趣享奖品队列处理异常" );
                    logger.error(e.getMessage(), e);
                    MailUtil.sendMail("趣享奖品发放异常",e);
                } finally {
                    queue.remove(event);
                }
            }
        }

    }

    @Override
    public Object getLock() {
        return EnjoyGoodsTask.OTHER_STATUS;
    }
}
